package com.itheima.order;

import com.itheima.order.domain.Order;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Producer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("group4");
        producer.setNamesrvAddr("192.168.23.130:9876");
        producer.start();
        ArrayList<Order> orderList = new ArrayList<>();
        orderList.add(new Order("a", "主单-1"));
        orderList.add(new Order("a", "子单-1"));
        orderList.add(new Order("a", "支付-1"));
        orderList.add(new Order("a", "推送-1"));
        orderList.add(new Order("b", "主单-1"));
        orderList.add(new Order("b", "子单-1"));
        orderList.add(new Order("b", "支付-1"));
        orderList.add(new Order("c", "主单-1"));
        orderList.add(new Order("c", "子单-1"));
        for (final Order order : orderList) {
            Message message = new Message("Topic4", "TagA", order.toString().getBytes());
            SendResult sendResult = producer.send(message, (list,msg,o)->
                    list.get(order.getOrderId().hashCode() % list.size()),null);
            System.out.println(sendResult);
        }
        producer.shutdown();
    }
}
